package org.opentcs.kcvehicle.communication.kc.udp.Service;

import org.opentcs.kcvehicle.communication.kc.udp.agv.param.AgvEvent;
import org.opentcs.kcvehicle.communication.kc.udp.agv.param.AgvEventConstant;
import org.opentcs.kcvehicle.communication.kc.udp.agv.param.function.x17.QueryRobotRunStatusRsp;
import org.opentcs.kcvehicle.communication.kc.udp.agv.param.rsp.RcvEventPackage;
import org.opentcs.kcvehicle.communication.kc.udp.io.UDPClient;

public class QryRobotRunStatus
    extends
      BaseCommand {

  /**
   * decs: 查询机器人运行状态.
   * 指令：0x17
   * author: caixiang
   * date: 2025/1/17 16:25
   */
  public static AgvEvent queryRobotRunStatus() {
    AgvEvent agvEvent = new AgvEvent(AgvEventConstant.CommandCode_QUERY_ROBOT_RUN_STATUS);
    return agvEvent;
  }

  public static QueryRobotRunStatusRsp command() {
    //0x17(查询机器人运行状态)
    AgvEvent agvEvent = queryRobotRunStatus();
    printInfo(agvEvent);
    RcvEventPackage rcv = UDPClient.localAGV.send(agvEvent);
    if (rcv.isOk()) {
      //QueryCargoStatusRsp queryCargoStatusRsp = new QueryCargoStatusRsp(rcv.getDataBytes());
      QueryRobotRunStatusRsp queryRobotRunStatusRsp = new QueryRobotRunStatusRsp(
          rcv.getDataBytes()
      );
      System.out.println(queryRobotRunStatusRsp.toString());
      System.out.println();
      System.out.println("received transationId : " + "isok:" + rcv.isOk());
      for (byte b : rcv.getValue()) {
        System.out.print(byteToHex(b) + " ");
      }
      return queryRobotRunStatusRsp;
    }
    else {
      System.out.println();
      System.out.println("received transationId : " + "isok:" + rcv.isOk());
      throw new RuntimeException("0x17 fail");
    }
  }
}
